const http = require("http");
const fs = require("fs");
const path = require("path");
const type = require(path.join(__dirname, "type.js"));

http.createServer(function (request, response) {
    let pathname = request.url;
    if (pathname === "/") {
        pathname = "index.html"
    }
    const extpath = path.extname(pathname);
    console.log(extpath);

    if (request.url !== "/favicon.ico") {

        fs.readFile(path.join(__dirname, "static/" + pathname), function (err, data) {
            if (err) {
                fs.readFile(path.join(__dirname, "static/404.html"), function (err, data404) {
                    if (err) {
                        console.log(err);
                    } else {
                        response.writeHead(200, {"Content-Type": "text/html;charset=utf-8"});
                        response.write(data404);
                        response.end()
                    }
                });
            } else {
                response.writeHead(200, {"Content-Type": `${type.getMime(extpath)};charset=utf-8`});
                response.write(data);
                response.end()
            }
        })
    }
    console.log(request.url);

}).listen(8001);
